package com.wt.order.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wt.order.common.BaseContext;
import com.wt.order.common.R;
import com.wt.order.entity.Orders;
import com.wt.order.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 吴涛
 * @date 2022/5/18 9:05
 * @qq 337144203
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    /**
     * 用户下单
     *
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> saveOrder(@RequestBody Orders orders) {
        log.info("orders={}", orders);
        ordersService.saveOrder(orders);
        return R.success("下单成功，即将为您派送");
    }


    /**
     * 查看下单信息
     *
     * @return
     */
    @GetMapping("/userPage")
    public R<Page> getOrders(int page, int pageSize) {
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        //构建查询条件，查询出自己的,然后根据下单时间进行排序
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId, BaseContext.getCurrentId());
        queryWrapper.orderByDesc(Orders::getOrderTime);
        ordersService.page(ordersPage, queryWrapper);
        return R.success(ordersPage);
    }

    /**
     * 后台管理断，查看订单信息
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */

    @GetMapping("/page")
    public R<Page> Orders(int page,int pageSize,String number,String beginTime,String endTime){

        Page<Orders> ordersPage = new Page<>(page, pageSize);
        //构建出查询构造器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(number!=null,Orders::getNumber,number);
        queryWrapper.gt(StringUtils.isNotEmpty(beginTime),Orders::getOrderTime,beginTime);
        queryWrapper.lt(StringUtils.isNotEmpty(endTime),Orders::getOrderTime,endTime);
        ordersService.page(ordersPage,queryWrapper);


        return R.success(ordersPage);
    }


    /**
     * 订单的派送
     * @param orders
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Orders orders){
        ordersService.updateById(orders);

        return R.success("订单已派送");
    }

}
